Kenneth Tay
Oct 16, 2018
ggplot2
dplyr
select
mutate
arrange
filter
summarize
group_by
readr
“Official” cheat sheet for readr
available here.
The most important syntax in R is the function call. All R syntax has function calls underlying it.
function_name(<inputs to the function>,
<arguments which change
how the function operates>)
x <- c(-5, -3, -1, 1, 3, NA)
mean(x, na.rm = TRUE)
## [1] -1
abs(x)
: If x
is positive, return x
. If x
is negative, return x
without the negative sign.
mean(abs(x), na.rm = TRUE)
## [1] 2.6
abs(x)
: If x
is positive, return x
. If x
is negative, return x
without the negative sign.
mean(abs(x), na.rm = TRUE)
## [1] 2.6
%>%
syntax with dplyr
Take the mtcars
dataset, select just the wt
and mpg
columns, then select rows with mpg < 15
mtcars %>%
select(wt, mpg) %>%
filter(mpg < 15)
+
syntax with ggplot2
library(ggplot2)
ggplot(data = mtcars, mapping = aes(x = wt, y = hp)) +
geom_point() +
labs(title = "Horsepower vs. Weight", x = "Weight",
y = "Horsepower") +
theme_classic()
dplyr
), you have to reload themgetwd()
None
to D4
: drought levels of increasing severity
Optional material
readr
)readxl
)haven
)DBI
)jsonlite
)xml2
)httr
)rvest
)tidyr
functions: gather
and spread
gather
: Used when some column names are not variables, but values of a variable
spread
: Opposite of gather
tidyr
functions: separate
and unite
separate
: Used to separate values in one column into multiple columns
unite
: Opposite of separate